﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web;

namespace WebApp
{
    public class ControllerBase
    {

        public ControllerBase()
        {
            Module = new HashWapper();
        }

        public HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
            
        }
        public HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }
        public HttpServerUtility Server
        {
            get
            {
                return HttpContext.Current.Server;
            }
        }

        public string SiteRoot
        {
            get
            {
                if (ThreadBaseData.Get()["sys_SiteRoot"] != null)
                {
                    return ThreadBaseData.Get()["sys_SiteRoot"].ToString();
                }
                else
                {
                    return "";
                }
            }
        }
        public SortedDictionary<string, string> Data
        {
            get
            {
                return (SortedDictionary<string, string>)ThreadBaseData.Get().Data["sys_ParamData"];
            }
        }
        public System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return HttpContext.Current.Session;
            }
        }
        public System.Web.HttpApplicationState Application
        {
            get
            {
                return HttpContext.Current.Application;
            }
        }

        public HashWapper Module
        {
            get;
            set;
        }
        public void RedirectToView(string Name)
        {
            Module["sys_ParamData"] = Data;
            string Path=SiteRoot + "/view/" + Name + ".aspx";
            Server.Execute(Path);
            Response.End();
        }
        public void ResponseJSON(IEnumerable List)
        {
            int index = 0;
            StringBuilder Json = new StringBuilder();
            Json.Append("[\r\n");
            Type ptp = null;
            foreach (Object o in List)
            {
                Json.Append("\t{\r\n");
                Type t = o.GetType();
                PropertyInfo[] pps = t.GetProperties();
                for (int i = 0; i < pps.Length; i++)
                {
                    Json.Append("\t\t");
                    ptp = pps[i].GetType();
                    if (ptp == typeof(String) || ptp == typeof(DateTime))
                    {
                        Json.Append(pps[i].Name).Append(":\"").Append(pps[i].GetValue(o, new object[] { })).Append("\"");
                    }
                    else
                    {
                        Json.Append(pps[i].Name).Append(":").Append(pps[i].GetValue(o, new object[] { }));
                    }
                    if (i < (pps.Length - 1))
                    {
                        Json.Append(",\r\n");
                    }
                }
                Json.Append("\t}\r\n");
                Json.Append("\t,\r\n");
            }
            Json.Length = Json.Length - 3;
            Json.Append("]\r\n");
            NoCache();
            Response.Write(Json.ToString());
            Response.Flush();
            Response.End();
        }

        public void NoCache()
        {
            Response.Buffer = true;
            Response.Expires = 0;
            Response.ExpiresAbsolute = DateTime.MinValue;
            Response.CacheControl = "no-cache";
        }
    }
}
